package com.spring.hot;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Cizai
 * @since 2022/12/02 10:23
 * @see <a href="https://leetcode.cn/problems/letter-combinations-of-a-phone-number/?favorite=2cktkvj">...</a>
 **/
public class 电话号码的字母组合 {

    List<String> result = new ArrayList<>();
    StringBuilder str = new StringBuilder();
    public List<String> letterCombinations(String digits) {

        if(digits==null || digits.length()==0) return result;

        String[] map = new String[] {"","","abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

        backtrack(digits,map,0);
        return result;
    }

    private void backtrack(String digits, String[] map, int start) {

        if (start == digits.length()) {
            result.add(str.toString());
            return;
        }

        char c = digits.charAt(start);
        String s = map[c-'0'];
        for (int i = 0; i < s.length(); i++) {
            char c1 = s.charAt(i);
            str.append(c1);
            backtrack(digits,map,start+1);
            str.deleteCharAt(str.length() - 1);
        }


    }
}
